/**
##############################################################################
#
# Copyright (C) 2020 Bonainfo <guoyihot@outlook.com>
# All Rights Reserved
#
##############################################################################
 */
odoo.define('iant_dynamic_list_form.SortDropdown', function(require) {

    "use strict";
    var core = require('web.core');
    var session = require('web.session');
    var Widget = require('web.Widget');
    var QWeb = core.qweb;
    var uid = session.uid;
    var _t = core._t;
    var framework = require('web.framework');
  
    var SortDropdown = Widget.extend({
    	template: 'dropdown_layout',
    	
    	init:function(parent,params){
    		this._super.apply(this,arguments);
    		this.config_field = params.state || [];
    		this.formField = params.formField || '';
    		this.config_list = params.listConfig || [];
    		
    	},
    	start:function(){  
    		var self = this;
    		self.$('.btn-config-clear').on('click',function(ev){self._onClearConfig(ev);});
    		self.$('.btn-config-clear').on('click',function(ev){self._onClearConfig(ev);});
    		self.$('.col-config-check').on('click',function(ev){self._onVisibleChange(ev);});
    		self.$('.field-width').on('change',function(ev){self._onWidthChange(ev);});
    		self.$('.list_config_value').on('change',function(ev){self._onListChange(ev);});
            self.$('.dropdown-menu').on('click',function(e){
            	e.stopPropagation();
            });
    		
    		self.$('.btn-config-apply').on('click',function(e){
    			self.trigger_up('config_save');
    		});
    		
            self.$('.field-width').on('click',function(e){
            	$(e.target).focus();
            	}
            );
            self.$('.col-config-check').each(function(){
            	var id = $(this).data('id');
            	var config = _.chain(self.config_field).filter(function(z){return z.id == id}).first().value()
            	if(!config){return;}
            	$(this).prop('checked',!config.invisible);
            });
            self.$('.dropdown-menu').sortable({
            	cancel: ".no-sort",
			    axis: "y",
			    items: "li:not(.no-sort)",
			    stop:function(ev,ui){
			    	var item_ids=self.$('.dropdown-menu').sortable( "toArray" );
			    	var sequence=0;
			    	_.each(item_ids,function(itemId){
			    		var id=self.$('#'+itemId).data('id');
			    		var block = !(sequence==_.size(item_ids)-1);
			    		self.trigger_up('config_change',{id:id,changes:{sequence:sequence}});
			    		sequence++;
			    	});
			    	
			    }
            });
		
    		return this._super.apply(this,arguments);
    	},
    	_onWidthChange:function(ev){
    		ev.stopPropagation();    		
    		var $item = $(ev.target);
    		var id = $item.data('id');
    		var value = parseFloat($item.val());
    		if(!isNaN(value)){
    			this.trigger_up('config_change',{id:id,changes:{width:value}});
    		}
    		
    		
    	},
    	_onVisibleChange:function(ev){
    		ev.stopPropagation();
    		var $item = $(ev.target);
    		var id = $item.data('id');
    		var value = $item.prop('checked');
    		this.trigger_up('config_change',{id:id,changes:{invisible:!value}});
    			
    	},
    	_onListChange:function(ev){
    		ev.stopPropagation();
    		var $item = $(ev.target);
    		var id = $item.data('id');
    		var value = $item.val();
    		this.trigger_up('config_change',{id:id,changes:{value:value}});
    	},
    	_onClearConfig:function(ev){
    		ev.stopPropagation();
    		this.trigger_up('delete_config');
    	}
    
    		
    }) ;
    
    
 

 return SortDropdown


});
